﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using GameLogic.Controls;

namespace GameLogic.Logic
{
    // Provides basic functionality for a behavior depending on the GameContainer Control
    public abstract class GameContainerBehavior<T> : Behavior<T> where T : FrameworkElement
    {
        private GameContainerFinder gameContainerFinder;
        private bool loaded;

        public FrameworkElement Element
        {
            get { return this.AssociatedObject; }
        }

        protected GameContainer GameContainer
        {
            get
            {
                return this.gameContainerFinder.GameContainer;
            }
        }

        //
        // Events
        //
        protected override void OnAttached()
        {
            base.OnAttached();

            this.gameContainerFinder = new GameContainerFinder(this.AssociatedObject);
            this.gameContainerFinder.OnGameContainerLoaded += this.OnGameContainerLoaded;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.gameContainerFinder = null;
        }

        private void OnGameContainerLoaded(object sender, EventArgs e)
        {
            if (!this.loaded)
            {
                this.OnGameContainerInitialized();
                this.loaded = true;
            }
        } 

        protected virtual void OnGameContainerInitialized()
        {
        }        
    }
}

